import {IsPositive,IsAlpha,IsEnum,IsBoolean} from 'class-validator'
import {JsonController,State,Ctx, Get, Post,Put, QueryParam, Delete, Body} from "routing-controllers";
import {Service} from "typedi";
import {UserService} from "../../service/user.service";
import User from "../../models/user/user";


enum Roles {
  Admin = "admin",
  User = "user",
  Guest = "guest",
}

class GetUsersQuery {

  @IsPositive()
  limit: number;

  @IsAlpha()
  city: string;

  @IsEnum(Roles)
  role: Roles;

  @IsBoolean()
  isActive: boolean;

}

@Service()
@JsonController()
export class UserController {

  constructor(private readonly userService: UserService) {
  }

  // @Get("/user")
  // all(): Promise<User[]> {
  //   return this.userService.findAll();
  // }

  @Get("/user")
  getUser(@QueryParam("id") id: number) {
    return this.userService.findOne(id);
  }

  //用户注册
  @Post("/user/register")
 async postUserRegister(@Body() user: User,@State() state:any) {
    let {send} = state
    let res = await this.userService.create(user)
    // ctx.send(res)
    // ctx.response.status = 201
    // ctx.response.body = res
    // ctx.response.data = res

    return send(res)
  }
  //用户登录
  @Post("/user/login")
  postUserLogin(@Body() user: User) {
    return this.userService.create(user);
  }

  @Put('/user')
  putUser(@Body() user:User){
    return this.userService.update(user)
  }

  @Delete("/user")
  deleteUser(@QueryParam("id") id: number) {
    return this.userService.remove(id);
  }

}
